home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / warnings.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2005-10-18  |  7.0 KB  |  287 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. '''Python part of the warnings subsystem.'''
  5. import sys
  6. import types
  7. import linecache
  8. __all__ = [
  9.     'warn',
  10.     'showwarning',
  11.     'formatwarning',
  12.     'filterwarnings',
  13.     'resetwarnings']
  14. filters = []
  15. defaultaction = 'default'
  16. onceregistry = { }
  17.  
  18. def warn(message, category = None, stacklevel = 1):
  19.     '''Issue a warning, or maybe ignore it or raise an exception.'''
  20.     if isinstance(message, Warning):
  21.         category = message.__class__
  22.     
  23.     if category is None:
  24.         category = UserWarning
  25.     
  26.     
  27.     try:
  28.         caller = sys._getframe(stacklevel)
  29.     except ValueError:
  30.         globals = sys.__dict__
  31.         lineno = 1
  32.  
  33.     globals = caller.f_globals
  34.     lineno = caller.f_lineno
  35.     if '__name__' in globals:
  36.         module = globals['__name__']
  37.     else:
  38.         module = '<string>'
  39.     filename = globals.get('__file__')
  40.     if filename:
  41.         fnl = filename.lower()
  42.         if fnl.endswith('.pyc') or fnl.endswith('.pyo'):
  43.             filename = filename[:-1]
  44.         
  45.     elif module == '__main__':
  46.         
  47.         try:
  48.             filename = sys.argv[0]
  49.         except AttributeError:
  50.             filename = '__main__'
  51.         except:
  52.             None<EXCEPTION MATCH>AttributeError
  53.         
  54.  
  55.     None<EXCEPTION MATCH>AttributeError
  56.     if not filename:
  57.         filename = module
  58.     
  59.     registry = globals.setdefault('__warningregistry__', { })
  60.     warn_explicit(message, category, filename, lineno, module, registry)
  61.  
  62.  
  63. def warn_explicit(message, category, filename, lineno, module = None, registry = None):
  64.     if module is None:
  65.         module = filename
  66.         if module[-3:].lower() == '.py':
  67.             module = module[:-3]
  68.         
  69.     
  70.     if registry is None:
  71.         registry = { }
  72.     
  73.     if isinstance(message, Warning):
  74.         text = str(message)
  75.         category = message.__class__
  76.     else:
  77.         text = message
  78.         message = category(message)
  79.     key = (text, category, lineno)
  80.     if registry.get(key):
  81.         return None
  82.     
  83.     for item in filters:
  84.         (action, msg, cat, mod, ln) = item
  85.         if (msg is None or msg.match(text)) and issubclass(category, cat):
  86.             if mod is None or mod.match(module):
  87.                 if ln == 0 or lineno == ln:
  88.                     break
  89.                     continue
  90.     else:
  91.         action = defaultaction
  92.     if action == 'ignore':
  93.         registry[key] = 1
  94.         return None
  95.     
  96.     if action == 'error':
  97.         raise message
  98.     
  99.     if action == 'once':
  100.         registry[key] = 1
  101.         oncekey = (text, category)
  102.         if onceregistry.get(oncekey):
  103.             return None
  104.         
  105.         onceregistry[oncekey] = 1
  106.     elif action == 'always':
  107.         pass
  108.     elif action == 'module':
  109.         registry[key] = 1
  110.         altkey = (text, category, 0)
  111.         if registry.get(altkey):
  112.             return None
  113.         
  114.         registry[altkey] = 1
  115.     elif action == 'default':
  116.         registry[key] = 1
  117.     else:
  118.         raise RuntimeError('Unrecognized action (%r) in warnings.filters:\n %s' % (action, item))
  119.     showwarning(message, category, filename, lineno)
  120.  
  121.  
  122. def showwarning(message, category, filename, lineno, file = None):
  123.     '''Hook to write a warning to a file; replace if you like.'''
  124.     if file is None:
  125.         file = sys.stderr
  126.     
  127.     
  128.     try:
  129.         file.write(formatwarning(message, category, filename, lineno))
  130.     except IOError:
  131.         pass
  132.  
  133.  
  134.  
  135. def formatwarning(message, category, filename, lineno):
  136.     '''Function to format a warning the standard way.'''
  137.     s = '%s:%s: %s: %s\n' % (filename, lineno, category.__name__, message)
  138.     line = linecache.getline(filename, lineno).strip()
  139.     if line:
  140.         s = s + '  ' + line + '\n'
  141.     
  142.     return s
  143.  
  144.  
  145. def filterwarnings(action, message = '', category = Warning, module = '', lineno = 0, append = 0):
  146.     '''Insert an entry into the list of warnings filters (at the front).
  147.  
  148.     Use assertions to check that all arguments have the right type.'''
  149.     import re
  150.     item = (action, re.compile(message, re.I), category, re.compile(module), lineno)
  151.     if append:
  152.         filters.append(item)
  153.     else:
  154.         filters.insert(0, item)
  155.  
  156.  
  157. def simplefilter(action, category = Warning, lineno = 0, append = 0):
  158.     '''Insert a simple entry into the list of warnings filters (at the front).
  159.  
  160.     A simple filter matches all modules and messages.
  161.     '''
  162.     item = (action, None, category, None, lineno)
  163.     if append:
  164.         filters.append(item)
  165.     else:
  166.         filters.insert(0, item)
  167.  
  168.  
  169. def resetwarnings():
  170.     '''Clear the list of warning filters, so that no filters are active.'''
  171.     filters[:] = []
  172.  
  173.  
  174. class _OptionError(Exception):
  175.     '''Exception used by option processing helpers.'''
  176.     pass
  177.  
  178.  
  179. def _processoptions(args):
  180.     for arg in args:
  181.         
  182.         try:
  183.             _setoption(arg)
  184.         continue
  185.         except _OptionError:
  186.             msg = None
  187.             print >>sys.stderr, 'Invalid -W option ignored:', msg
  188.             continue
  189.         
  190.  
  191.     
  192.  
  193.  
  194. def _setoption(arg):
  195.     import re
  196.     parts = arg.split(':')
  197.     if len(parts) > 5:
  198.         raise _OptionError('too many fields (max 5): %r' % (arg,))
  199.     
  200.     while len(parts) < 5:
  201.         parts.append('')
  202.     (action, message, category, module, lineno) = [ s.strip() for s in parts ]
  203.     action = _getaction(action)
  204.     message = re.escape(message)
  205.     category = _getcategory(category)
  206.     module = re.escape(module)
  207.     if lineno:
  208.         
  209.         try:
  210.             lineno = int(lineno)
  211.             if lineno < 0:
  212.                 raise ValueError
  213.         except (ValueError, OverflowError):
  214.             None if module else []
  215.             None if module else []
  216.             raise _OptionError('invalid lineno %r' % (lineno,))
  217.         except:
  218.             None if module else []<EXCEPTION MATCH>(ValueError, OverflowError)
  219.         
  220.  
  221.     None if module else []
  222.     lineno = 0
  223.     filterwarnings(action, message, category, module, lineno)
  224.  
  225.  
  226. def _getaction(action):
  227.     if not action:
  228.         return 'default'
  229.     
  230.     if action == 'all':
  231.         return 'always'
  232.     
  233.     for a in [
  234.         'default',
  235.         'always',
  236.         'ignore',
  237.         'module',
  238.         'once',
  239.         'error']:
  240.         if a.startswith(action):
  241.             return a
  242.             continue
  243.     
  244.     raise _OptionError('invalid action: %r' % (action,))
  245.  
  246.  
  247. def _getcategory(category):
  248.     import re
  249.     if not category:
  250.         return Warning
  251.     
  252.     if re.match('^[a-zA-Z0-9_]+$', category):
  253.         
  254.         try:
  255.             cat = eval(category)
  256.         except NameError:
  257.             raise _OptionError('unknown warning category: %r' % (category,))
  258.         except:
  259.             None<EXCEPTION MATCH>NameError
  260.         
  261.  
  262.     None<EXCEPTION MATCH>NameError
  263.     i = category.rfind('.')
  264.     module = category[:i]
  265.     klass = category[i + 1:]
  266.     
  267.     try:
  268.         m = __import__(module, None, None, [
  269.             klass])
  270.     except ImportError:
  271.         raise _OptionError('invalid module name: %r' % (module,))
  272.  
  273.     
  274.     try:
  275.         cat = getattr(m, klass)
  276.     except AttributeError:
  277.         raise _OptionError('unknown warning category: %r' % (category,))
  278.  
  279.     if not isinstance(cat, types.ClassType) or not issubclass(cat, Warning):
  280.         raise _OptionError('invalid warning category: %r' % (category,))
  281.     
  282.     return cat
  283.  
  284. _processoptions(sys.warnoptions)
  285. simplefilter('ignore', category = OverflowWarning, append = 1)
  286. simplefilter('ignore', category = PendingDeprecationWarning, append = 1)
  287.